using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class drag_slot : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler, IPointerUpHandler
{
    Vector3 original_position;
    // GameObject tmp_father;
    GameObject original_father;

    GameObject guard_slot;
    Vector3 original_guard_slot_position;

    public CreateImg create_image_comp;
    // drag_bag drag_bag_comp;
    public GameObject bag_obj;

    void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
    {
        //Grab the number of consecutive clicks and assign it to an integer varible.
        // BdUtils.log("eventData.clickCount --- ", eventData.clickCount);

        // BdUtils.log("eventData.button --- ", eventData.button);
        BdUtils.ReflectionUtils.get_properties(eventData);

        int slot_index_1 = get_obj_index_by_name(gameObject.name);
        string desc = create_image_comp.get_discription_text_by_slot_index(slot_index_1);
        // BdUtils.log(" --- create_image_comp: ", create_image_comp);
        // BdUtils.log(" --- create_image_comp.item_description_comp: ", create_image_comp.item_description_comp);
        create_image_comp.item_description_comp.text = desc;
        create_image_comp.current_slot_index = slot_index_1;
    }

    void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
    {

    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (gameObject.name.StartsWith("empty_"))
            return;

        // BdUtils.log(" --- OnBeginDrag", gameObject.name);

        original_position = transform.position;

        original_father = gameObject.transform.parent.gameObject;

        int slot_index_1 = get_obj_index_by_name(gameObject.name);
        var row_1 = create_image_comp.get_bag_row_i(slot_index_1);
        // create_image_comp.get_slot_info_by_row_i()
        // (img_file_name, item_number, is_prefab) = create_image_comp.get_slot_info_by_row_i(row_1);
        var (img_file_name, item_number, is_prefab) = create_image_comp.get_slot_info_by_row_i(row_1);

        create_image_comp.set_one_slot(guard_slot, img_file_name, item_number, is_prefab, slot_name: guard_slot.name, slot_index: slot_index_1, row_i: row_1);

        // create_image_comp.set_one_slot(guard_slot, )

        // transform.SetParent(tmp_father.transform);

        GetComponent<CanvasGroup>().blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (gameObject.name.StartsWith("empty_"))
            return;
        // originalParent = transform.parent;
        transform.position = eventData.position;
        // transform
        transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
        // BdUtils.log(" --- OnDrag transform.position :", transform.position);

        guard_slot.transform.position = transform.position;
    }

    public int get_obj_index_by_name(string obj_name, string sep = "_")
    {
        int ret = -1;
        var ls = obj_name.Split(sep);
        try
        {
            var _ret = BdUtils.conv_to_int(ls[ls.Length - 1]);
            ret = (int)_ret;
        }
        catch (UnityException e)
        {
            BdUtils.log($"*** get_obj_index_by_name({obj_name}) throw error: {e}");
        }
        return ret;
    }
    void IEndDragHandler.OnEndDrag(PointerEventData eventData)
    {
        if (gameObject.name.StartsWith("empty_"))
            return;
        // BdUtils.log(" --- OnEndDrag", gameObject.name);

        bool success_flag = false;

        GameObject ui_obj = BdTools.UITools.get_ui_object_on_position(Input.mousePosition);
        // BdUtils.log(obj);
        if (ui_obj != null)
        {

            // string path = GameObjectCopyPath.GetTransPath(ui_obj);

            // BdUtils.log($" ~~~ OnEndDrag ui_obj.path: {path.Contains("open_bag")} --- ", path);

            string ui_obj_path = GameObjectCopyPath.GetTransPath(ui_obj);

            if (ui_obj_path.Contains("/grid/empty_") || ui_obj_path.Contains("/grid/slot_"))
            {

                int slot_index_1 = get_obj_index_by_name(gameObject.name);
                int slot_index_2 = get_obj_index_by_name(ui_obj.name);
                BdUtils.log("swap_slot --- slot_index_1: ", slot_index_1, "--- slot_index_2:", slot_index_2);


                create_image_comp.current_slot_index = slot_index_2;

                // if (ui_obj_path.Contains("/grid/empty_"))
                // {
                //     BdUtils.log($" ~~~ OnEndDrag: 您将[{gameObject.name}]拖拽到了空物体[{ui_obj.name}]上~");

                //     // transform.position = ui_obj.transform.position;

                //     // var x = gameObject.name.Split("_")[1];


                // }
                // else if (ui_obj_path.Contains("/grid/slot_"))
                // {
                //     BdUtils.log($" ~~~ OnEndDrag: 您将[{gameObject.name}]拖拽到了`item`[{ui_obj.name}]上~");
                // }

                if (slot_index_1 != -1 && slot_index_2 != -1)
                {
                    success_flag = true;
                    create_image_comp.swap_slot(slot_index_1, slot_index_2);
                }
                // else
                //     transform.position = original_position;
            }
            // else
            // {
            //     transform.position = original_position;
            // }

            // BdUtils.log(" --- ui_obj_path:", ui_obj_path);
        }
        // else
        // {
        //     transform.position = original_position;
        // }

        if (!success_flag)
            transform.position = original_position;
        GetComponent<CanvasGroup>().blocksRaycasts = true;

        guard_slot.transform.position = original_guard_slot_position;
        // BdUtils.log(" original_guard_slot_position --- ", original_guard_slot_position);

        // original_father = gameObject.transform.parent.gameObject;
        // transform.SetParent(original_father.transform);
    }

    private void Awake()
    {
        // GameObject create_img_obj = GameObject.Find("/Canvas/btns/create_img");
        // GameObject create_img_obj = transform.Find("/Canvas/btns/create_img").gameObject;

        // var drag_bag_comp = GetComponentInParent<drag_bag>();
        // var bag_obj = GameObject.Find("/Canvas/Bag");
    }

    // Start is called before the first frame update
    void Start()
    {
        var bag_obj = gameObject.GetComponentInParent<BagEnable>().gameObject;
        // var bag_obj = BdTools.get_game_object_by_path("/Canvas/Bag");
        BdUtils.assert(bag_obj, "`bag_obj`不能为空!");
        create_image_comp = bag_obj.GetComponentInChildren<CreateImg>();
        BdUtils.assert(bag_obj, "`create_image_comp`不能为空!");

        // GameObject create_img_obj = drag_bag_comp.transform.Find("create_img").gameObject;
        // create_image_comp = create_img_obj.GetComponent<CreateImg>();

        // guard_slot = GameObject.Find("/Canvas/btns/guard_slot");
        guard_slot = create_image_comp.guard_slot;
        original_guard_slot_position = create_image_comp.original_guard_slot_position;
        // GetComponent<CanvasGroup>().blocksRaycasts = true;
    }

    // Update is called once per frame
    void Update()
    {

    }
}
